//package cn.liukuan.jps.redis;
//
//import org.apache.lucene.store.*;
//
//import java.io.Serializable;
//import java.util.HashSet;
//import java.io.IOException;
//
//
///**
// * Created by liukuan on 16/5/30.
// */
//public class RedisRAMSingleInstanceLockFactory extends LockFactory implements Serializable {
//
//    final HashSet<String> locks = new HashSet<String>();
//
//    @Override
//    public Lock obtainLock(Directory dir, String lockName) throws IOException {
//        synchronized (locks) {
//            if (locks.add(lockName)) {
//                return new SingleInstanceLock(lockName);
//            } else {
//                throw new LockObtainFailedException("lock instance already obtained: (dir=" + dir + ", lockName=" + lockName + ")");
//            }
//        }
//    }
//
//    private class SingleInstanceLock extends Lock {
//        private final String lockName;
//        private volatile boolean closed;
//
//        public SingleInstanceLock(String lockName) {
//            this.lockName = lockName;
//        }
//
//        @Override
//        public void ensureValid() throws IOException {
//            if (closed) {
//                throw new AlreadyClosedException("Lock instance already released: " + this);
//            }
//            // check we are still in the locks map (some debugger or something crazy didn't remove us)
//            synchronized (locks) {
//                if (!locks.contains(lockName)) {
//                    throw new AlreadyClosedException("Lock instance was invalidated from map: " + this);
//                }
//            }
//        }
//
//        @Override
//        public synchronized void close() throws IOException {
//            if (closed) {
//                return;
//            }
//            try {
//                synchronized (locks) {
//                    if (!locks.remove(lockName)) {
//                        throw new AlreadyClosedException("Lock was already released: " + this);
//                    }
//                }
//            } finally {
//                closed = true;
//            }
//        }
//
//        @Override
//        public String toString() {
//            return super.toString() + ": " + lockName;
//        }
//    }
//}
